package com.ywgt;

import java.util.Arrays;

public class Maze {
    static int [][] maze=new int[7][7];
    public static void main(String[] args) {
        //迷宫地图绘制
        for (int i=0;i<7;i++){
            maze [0][i]=1;
            maze [6][i]=1;
        }
        for (int i=0;i<7;i++){
            maze [i][0]=1;
            maze [i][6]=1;
        }
        maze[1] [2]=1;
        maze[1] [3]=1;
        maze[1] [4]=1;
        maze[3] [1]=1;
        maze[3] [2]=1;
        maze[3] [4]=1;
        maze[3] [5]=1;
        maze[4] [4]=1;
        maze[5] [2]=1;
        //打印地图
        print();
        System.out.println();
        walk(maze,1,1);
        //打印出迷宫的地图
        print();
    }

    /**
     * 约定走到maze[6][5]退出,0为可以走的路，1为墙，2为已经走过的路，3为死路
     * @param i
     * @param j
     * @return
     */
    public static boolean walk(int [] [] map,int i,int j){
        if(2==map[5][5]){
            return true;
        }else {
            if (map[i][j] == 0) { //如果这个点没走过
                map[i][j] = 2; //假定为2
                if (walk(map,i -1, j)) {//向上
                    return true;
                }
                if (walk(map,i, j + 1)) {//向右
                    return true;
                }
                if (walk(map,i +1, j)) {//向下
                    return true;
                }
                if (walk(map,i, j - 1)) {//向左
                    return true;
                } else {
                    map[i][j] = 3;
                    return false;
                }
            } else {
                return false;
            }
        }
    }


    public static void print(){
        for (int[] ints : maze) {
            System.out.println(Arrays.toString(ints));
        }
    }
}
